package com.paiai.lslease.action.appuser;

import com.paiai.lslease.auto.model.AppSysUser;
import com.paiai.lslease.base.AppResult;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.dao.appuser.AppSysUserInfoDAO;
import com.paiai.lslease.model.AppSysUserInfo;
import com.paiai.lslease.service.appuser.AppSysUserService;
import com.paiai.lslease.util.CommonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AppSysUserAction extends BaseAction {
    private AppSysUserService appSysUserService;
    private AppSysUserInfoDAO appSysUserInfoDAO;
    private String userId;
    private String userMobile;
    private String wechatOpenId;
    private String qq;
    private String nickName;
    private String photo;
    private String password;
    private String email;
    private String birthday;

    public void setAppSysUserService(AppSysUserService appSysUserService) {
        this.appSysUserService = appSysUserService;
    }

    public void setAppSysUserInfoDAO(AppSysUserInfoDAO appSysUserInfoDAO) {
        this.appSysUserInfoDAO = appSysUserInfoDAO;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setUserMobile(String userMobile) {
        this.userMobile = userMobile;
    }

    public void setWechatOpenId(String wechatOpenId) {
        this.wechatOpenId = wechatOpenId;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    /**
     * 根据微信OPENID验证用户状态 1 用户状态正常,请直接根据微信openid获取用户详情；-1 未注册，请引导用户绑定微信账号; 2 用户已标记为禁用；4 用户已标记为删除
     */
    public void checkAppUserStateByWechatOpenId() {
        try {
            if (StringUtils.isBlank(wechatOpenId)) {
                sendFailureMsg("微信openid不能为空");
                return;
            }
            //1 用户状态正常,请直接根据微信openid获取用户详情；-1 未注册，请引导用户绑定微信账号; 2 用户已标记为禁用；4 用户已标记为删除
//            int state = appSysUserService.checkAppUserStateByWechatOpenId(wechatOpenId);
            int state = -1;
            String stateDesc = "未注册，请引导用户绑定微信账号";
            AppSysUser appSysUser = appSysUserService.getAppSysUserInfoByWechatOpenId(wechatOpenId);
            if (appSysUser != null) {
                state = appSysUser.getState();
                if (state == 1) {
                    if (appSysUser.getLoginstate() != null && appSysUser.getLoginstate() == 1) {
                        state = 8;
                        stateDesc = "用户已登录";
                    } else {
                        state = 16;
                        stateDesc = "用户未登录";
                    }
                }
            }
//            sendResultMsg(new AppResult().setStatus(state).setData(appSysUser).setMsg(stateDesc));
            sendSuccesseMsg(state, appSysUser, stateDesc);
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("根据微信OPENID验证用户状态失败");
        }
    }

    /**
     * 获取用户详情,可根据用户ID或者微信openid或者用户手机号码来查询
     */
    public void getAppSysUserInfo() {
        try {
            if (StringUtils.isEmpty(userId + "") && StringUtils.isEmpty(wechatOpenId) && StringUtils.isEmpty(userMobile)) {
                sendFailureMsg("用户ID、微信openid、用户手机号码不能同时为空");
                return;
            }
            AppSysUser appSysUserInfo;
            if (StringUtils.isNotEmpty(userId)) {
//                appSysUserInfo = appSysUserService.getAppSysUserInfoById(Integer.parseInt(userId));
                appSysUserInfo = appSysUserInfoDAO.getAppSysUserInfoById(Integer.parseInt(userId));
            } else if (StringUtils.isNotEmpty(wechatOpenId)) {
//                appSysUserInfo = appSysUserService.getAppSysUserInfoByWechatOpenId(wechatOpenId);
                appSysUserInfo = appSysUserInfoDAO.getAppSysUserInfoByWechatOpenId(wechatOpenId);
            } else {
//                appSysUserInfo = appSysUserService.getAppSysUserInfoByMobile(userMobile);
                appSysUserInfo = appSysUserInfoDAO.getAppSysUserInfoByMobile(userMobile);
            }
            if (appSysUserInfo == null) {
                sendFailureMsg("获取系统用户详情失败");
                return;
            }
            sendSuccesseMsg(appSysUserInfo, "获取系统用户详情成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("获取系统用户详情失败");
        }
    }

    /**
     * 绑定微信账号
     */
    public void bindWXAcount() {
        try {
            if (StringUtils.isBlank(userMobile)) {
                sendFailureMsg("手机号码不能为空");
                return;
            }
            if (StringUtils.isEmpty(password)) {
                sendFailureMsg("密码不能为空");
                return;
            }
            if (password.length() > 20) {
                sendFailureMsg("密码不能超过20个字符");
                return;
            }
            if (StringUtils.isBlank(wechatOpenId)) {
                sendFailureMsg("微信openId不能为空");
                return;
            }
            if (appSysUserService.axaccountBinded(userMobile, wechatOpenId)) {//用户是否绑定过
                sendFailureMsg("用户已绑定过，请不要重复绑定");
                return;
            }
            AppSysUser appSysUser1 = appSysUserService.getAppSysUserInfoByMobile(userMobile);
            if (appSysUser1 != null && !wechatOpenId.equalsIgnoreCase(appSysUser1.getWechatOpenId())) {
                sendFailureMsg("用户已被其他人绑定，不能继续绑定");
                return;
            }
            AppSysUser appSysUser = new AppSysUser();
            appSysUser.setMobile(userMobile);
            appSysUser.setWechatOpenId(wechatOpenId);
            if (StringUtils.isEmpty(nickName)) {
                nickName = "用户" + appSysUser.getMobile().substring(appSysUser.getMobile().length() - 4);
            }
            appSysUser.setNickName(nickName);
            appSysUser.setPhoto(photo);
            appSysUser.setQq(qq);
            appSysUser.setEmail(email);
            appSysUser.setPassword(CommonUtil.encryptMD5(password));
            appSysUser.setState(1);
            appSysUser.setLoginstate(-1);
            appSysUser.setInsertTime(new Date());
            appSysUser.setUpdateTime(new Date());
            appSysUser.setRemark("绑定微信账号");
            AppSysUser appSysUserInfo;
            appSysUserInfo = appSysUserService.bindWXAccount(appSysUser);
            if (appSysUserInfo == null) {
                sendFailureMsg("绑定微信账号，查询账号信息失败");
                return;
            }
            sendSuccesseMsg(appSysUserInfo, "绑定微信账号成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("绑定微信账号失败");
        }
    }

    /**
     * 用户登录，可根据微信openid或者手机号码登录
     */
    public void login() {
        try {
            if (StringUtils.isEmpty(wechatOpenId) && StringUtils.isEmpty(userMobile)) {
                sendFailureMsg("登录失败，微信openid、用户手机号码不能同时为空");
                return;
            }
            if (StringUtils.isEmpty(password)) {
                sendFailureMsg("登录失败，密码不能为空");
                return;
            }
            AppSysUser appSysUserInfo;
            AppSysUser appSysUser1;
            if (StringUtils.isNotEmpty(wechatOpenId)) {
                appSysUserInfo = appSysUserService.getAppSysUserInfoByWechatOpenId(wechatOpenId);
                appSysUser1 = appSysUserInfoDAO.getAppSysUserInfoByWechatOpenId(wechatOpenId);
            } else {
                appSysUserInfo = appSysUserService.getAppSysUserInfoByMobile(userMobile);
                appSysUser1 = appSysUserInfoDAO.getAppSysUserInfoByMobile(userMobile);
            }
            if (appSysUserInfo == null) {
                sendFailureMsg("登录失败，不存在该用户，请先注册");
                return;
            }
            if (!CommonUtil.encryptMD5(password).equalsIgnoreCase(appSysUserInfo.getPassword())) {
                sendFailureMsg("登录失败，用户密码错误");
                return;
            }
            appSysUserInfo.setLoginstate(1);
            appSysUserService.modifyUserInfoByPrimaryKey(appSysUserInfo);
            sendSuccesseMsg(appSysUser1, "登录成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("登录失败，系统异常");
        }
    }

    /**
     * 修改用户基本信息
     */
    public void modifyUserInfo() {
        try {
            if (StringUtils.isEmpty(userId + "") && StringUtils.isEmpty(wechatOpenId) && StringUtils.isEmpty(userMobile)) {
                sendFailureMsg("修改用户基本信息失败，用户ID、微信openid、用户手机号码不能同时为空");
                return;
            }
            AppSysUser appSysUserInfo;
            if (StringUtils.isNotEmpty(userId)) {
                appSysUserInfo = appSysUserService.getAppSysUserInfoById(Integer.parseInt(userId));
            } else if (StringUtils.isNotEmpty(wechatOpenId)) {
                appSysUserInfo = appSysUserService.getAppSysUserInfoByWechatOpenId(wechatOpenId);
            } else {
                appSysUserInfo = appSysUserService.getAppSysUserInfoByMobile(userMobile);
            }
            if (appSysUserInfo == null) {
                sendFailureMsg("修改用户基本信息失败，获取系统用户详情失败");
                return;
            }
            if (StringUtils.isEmpty(qq) && StringUtils.isEmpty(nickName) && StringUtils.isEmpty(photo) && StringUtils.isEmpty(password)
                        && StringUtils.isEmpty(email) && StringUtils.isEmpty(birthday)) {
                sendFailureMsg("修改用户基本信息失败，qq、昵称、图像、密码、邮箱、生日请至少修改一样");
                return;
            }
            if (StringUtils.isNotEmpty(qq)) {
                if (!StringUtils.isNumeric(qq)) {
                    sendFailureMsg("修改用户基本信息失败，qq号码必须是纯数字");
                    return;
                }
                appSysUserInfo.setQq(qq);
            }
            if (StringUtils.isNotEmpty(nickName)) {
                appSysUserInfo.setNickName(nickName);
            }
            if (StringUtils.isNotEmpty(photo)) {
                if (!StringUtils.startsWith(photo, "http://") && !StringUtils.startsWith(photo, "https://")) {
                    sendFailureMsg("修改用户基本信息失败, 用户图像地址无法访问");
                    return;
                }
                appSysUserInfo.setPhoto(photo);
            }
            if (StringUtils.isNotEmpty(password)) {
                appSysUserInfo.setPassword(CommonUtil.encryptMD5(password));
            }
            if (StringUtils.isNotEmpty(email)) {
                appSysUserInfo.setEmail(email);
            }
            if (StringUtils.isNotEmpty(birthday)) {
                try {
                    Date birthdayDate = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(birthday);
                    appSysUserInfo.setBirthday(birthdayDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                    sendFailureMsg("修改用户基本信息失败，用户生日信息格式错误，请参考yyyy-MM-dd");
                    return;
                }
            }
            AppSysUser appSysUserModify = appSysUserService.modifyUserInfoByPrimaryKey(appSysUserInfo);
            if (appSysUserModify == null) {
                sendFailureMsg("修改用户基本信息失败，系统异常");
                return;
            }
            appSysUserInfo.setUpdateTime(new Date());
            sendSuccesseMsg(appSysUserModify, "修改用户基本信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("登录失败，系统异常");
        }
    }

    /**
     * 用户注销，可根据用户ID或者微信openid或者手机号码登录
     */
    public void logout() {
        try {
            if (StringUtils.isEmpty(userId) && StringUtils.isEmpty(wechatOpenId) && StringUtils.isEmpty(userMobile)) {
                sendFailureMsg("注销失败，用户ID、微信openid、用户手机号码不能同时为空");
                return;
            }
            AppSysUser appSysUserInfo;
            if (StringUtils.isNotEmpty(userId)) {
                if (!StringUtils.isNumeric(userId)) {
                    sendFailureMsg("注销失败，用户ID必须是纯数字");
                    return;
                }
                appSysUserInfo = appSysUserService.getAppSysUserInfoById(Integer.parseInt(userId));
            } else if (StringUtils.isNotEmpty(wechatOpenId)) {
                appSysUserInfo = appSysUserService.getAppSysUserInfoByWechatOpenId(wechatOpenId);
            } else {
                appSysUserInfo = appSysUserService.getAppSysUserInfoByMobile(userMobile);
            }
            if (appSysUserInfo == null) {
                sendFailureMsg("注销失败，不存在该用户");
                return;
            }
            appSysUserInfo.setLoginstate(-1);
            appSysUserService.modifyUserInfoByPrimaryKey(appSysUserInfo);
            sendSuccesseMsg(appSysUserInfo, "注销成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("注销失败，系统异常");
        }
    }
}
